/**
 * @filename:SysFieldServiceImpl 2022年1月27日
 * @project Wonder wallet Framework  V1.0
 * Copyright(c) 2018 LuZhongren Co. Ltd. 
 * All right reserved. 
 */
package com.wonder.admin.service.impl;

import com.wonder.admin.domain.SysField;
import com.wonder.admin.mapper.SysFieldDao;
import com.wonder.admin.service.SysFieldService;
import com.wonder.admin.request.SysFieldRequest;
import com.wonder.common.utils.StringUtils;

import jnr.ffi.Struct.int16_t;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;


/**   
 * @Description:TODO(模块字段管理服务实现)
 *
 * @version: V1.0
 * @author: LuZhongren
 * 
 */
@Service
public class SysFieldServiceImpl  extends ServiceImpl<SysFieldDao, SysField> implements SysFieldService  {

   /**
     * 修改模块字段管理信息
     * 
     * @param SysField信息
     * @return 结果
     */
    @Override
    public int updateEx(SysField sysField){
       if(sysField!=null)
		     return this.baseMapper.updateById(sysField);
       return 0;
    }
    
    /**
     * 更新字段sortid和字段ID一致
     * @param menu_id
     * @return
     */
    @Override
    public int updateBatchSortid(Long menu_id) {
    	List<SysField> list=this.list(new QueryWrapper<SysField>().eq("menu_id", menu_id));
    	int ret=0;
    	for(SysField sysField:list) {
    		if(sysField.getSortid()==null || sysField.getSortid()==0) {
    			ret +=this.update(new UpdateWrapper<SysField>().set("sortid", sysField.getId()).eq("id", sysField.getId()))?1:0;
    		}
    	}
    	return ret;//this.baseMapper.updateBatchSortId(menu_id);
		
	}
    
    
    
    
    
    /**
	 * Description:模块字段管理分页返回
	 * @request:检索条件
	 * @page：分页参数
	 * @return:分页记录
	 */
	@Override 
	public IPage<SysField> selectList(SysFieldRequest request, Page<SysField> page){
	    LambdaQueryWrapper<SysField> lambdaQueryWrapper = new LambdaQueryWrapper<>();
	     
	    if (null != request.getMenu_id()) {
	       lambdaQueryWrapper.eq(SysField::getMenu_id, request.getMenu_id());
	      }               

		lambdaQueryWrapper.orderByAsc(SysField::getSortid);	
		Page<SysField> selectPage = this.baseMapper.selectPage(page, lambdaQueryWrapper);

		return selectPage;
    }





	@Override
	public List<Map<String, Object>> getColumnMapList(String tableName) {
		// TODO Auto-generated method stub
		return this.baseMapper.getColumnMapList(tableName);
	}
	
	@Override
	public List<String> getFieldList(Long menu_id) {
		//There are two ways to return one column list
		 return this.listObjs(new QueryWrapper<SysField>().eq("menu_id", menu_id).eq("post_status", 1)
	                .lambda().select(SysField::getField), Object::toString);
		 
//	    LambdaQueryWrapper<SysField> lambdaQueryWrapper =
//	        new LambdaQueryWrapper<>();
//	    lambdaQueryWrapper
//	        .select(SysField::getField);  
//	    lambdaQueryWrapper.eq(SysField::getMenu_id, menu_id);
//  
//	    return this.baseMapper.selectObjs(lambdaQueryWrapper).stream()
//	        .map(o -> (String) o)
//	        .collect(Collectors.toList());
	  }
	
	@Override
	public List<String> getAllField(Long menu_id) {
		//There are two ways to return one column list
		 return this.listObjs(new QueryWrapper<SysField>().eq("menu_id", menu_id)
	                .lambda().select(SysField::getField), Object::toString);
		 
//	    LambdaQueryWrapper<SysField> lambdaQueryWrapper =
//	        new LambdaQueryWrapper<>();
//	    lambdaQueryWrapper
//	        .select(SysField::getField);  
//	    lambdaQueryWrapper.eq(SysField::getMenu_id, menu_id);
//  
//	    return this.baseMapper.selectObjs(lambdaQueryWrapper).stream()
//	        .map(o -> (String) o)
//	        .collect(Collectors.toList());
	  }
	/**
	 * 用show columns 获取表的列数据
	 * @param tableName
	 * @return
	 */
	@Override
	public List<Map<String,Object>> getTableFields(String tableName){
		return this.baseMapper.getTableFields(tableName);
	}





	@Override
	public List<Map<String, Object>> getTables() {
		return this.baseMapper.getTables();
	}
	
	/**
	 * 获取数据库表，去除系统表
	 * @return
	 */
	@Override
	public List<String> getTableList(){
		List<Map<String, Object>> list=this.baseMapper.getTables();
		String[] no_shows= {"sys_menu","sys_field","sys_application","sys_action","sys_oper_log","sys_logininfor"};
		List<String> tables=new ArrayList<String>();
		for(Map<String, Object> item:list) {
			 tables.add(item.get("TABLE_NAME").toString());	
		}
		for(int i=0;i<no_shows.length;i++ )
			if(tables.contains(no_shows[i])) tables.remove(no_shows[i]);
		
		return tables;
	}

    
	
}